package com.db.williamchart.animation;

import com.db.williamchart.data.DataPoint;
import ohos.agp.animation.AnimatorValue;

import java.util.List;

public class DefaultHorizontalAnimation extends ChartAnimation<DataPoint> {
    @Override
    public ChartAnimation<DataPoint> animateFrom(Float startPosition, List<DataPoint> entries, ChartCallback chartCallback) {
        for (DataPoint dataPoint : entries){
            float value = dataPoint.getScreenPositionX() - startPosition;

            AnimatorValue animator = new AnimatorValue();
            animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                @Override
                public void onUpdate(AnimatorValue animatorValue, float v) {
                    dataPoint.setScreenPositionX(v * value + startPosition);
                }
            });
            animator.setDuration(getDuration());
            animator.setCurveType(getCurveType());
            animator.setLoopedCount(0);
            animator.start();
        }

        AnimatorValue animatorValue = new AnimatorValue();
        animatorValue.setValueUpdateListener((animatorValue1, v) -> chartCallback.invoke(v));
        animatorValue.setDuration(getDuration());
        animatorValue.setCurveType(getCurveType());
        animatorValue.setLoopedCount(0);
        animatorValue.start();
        return this;
    }
}
